package cn.anecansaitin.whimsy.client.effect;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.player.LocalPlayer;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.ViewportEvent;

import java.util.Random;

@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = Whimsy.MODID)
public class EarthquakeClientEvent {
    private static final Random random = new Random();

    @SubscribeEvent
    public static void shake(ViewportEvent.ComputeCameraAngles event) {
        Minecraft mc = Minecraft.getInstance();
        LocalPlayer player = mc.player;

        if (player.hasEffect(ModEffects.EARTHQUAKE.get()) && player.level().getGameTime() % 2 == 0) {
            //当单人游戏暂停时，停止抖动
            if (mc.hasSingleplayerServer() && !mc.getSingleplayerServer().isPublished() && mc.screen instanceof PauseScreen) {
                return;
            }

            float scale = mc.options.screenEffectScale().get().floatValue();

            if (scale > 0) {
                int amplifier = player.getEffect(ModEffects.EARTHQUAKE.get()).getAmplifier() + 1;
                float range = 0.1f * amplifier * scale;
                event.setRoll(event.getRoll() + random.nextFloat(-range, range));
                event.setYaw(event.getYaw() + random.nextFloat(-range, range));
            }
        }
    }
}
